﻿using System;
using Microsoft.Practices.ObjectBuilder2;

namespace Lilium.Unity
{
	public static class BuilderContextExtensions
	{
		public static TPolicy GetPolicy<TPolicy>(this IBuilderContext context, bool required)
			where TPolicy: IBuilderPolicy
		{
			if (context == null) throw new ArgumentNullException("context");

			var policy = context.Policies.Get<TPolicy>(context.BuildKey);
			
			if ((policy == null) && required)
				throw new InvalidOperationException(string.Format("Required policy of type {0} not found.", typeof(TPolicy)));

			return policy;
		}
	}
}
